package com.example.weatherforecast.whtrcdn.adapter

import android.app.AlertDialog
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.weatherforecast.R
import com.example.weatherforecast.whtrcdn.WhtrcdnViewModel
import com.example.weatherforecast.whtrcdn.entity.WeatherCityInfo

class WeatherCityManagerListAdapter(private val context: Context, private var data: List<WeatherCityInfo>, val viewMode: WhtrcdnViewModel): RecyclerView.Adapter<WeatherCityManagerListAdapter.CityViewHolder>() {
    private val TAG: String = WeatherCityManagerListAdapter::class.java.simpleName
    class CityViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val cityName: TextView = itemView.findViewById(R.id.tv_weather_city_manager_city_name)
        val cityDelete: ImageView = itemView.findViewById(R.id.iv_weather_city_manager_city_delete)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CityViewHolder {
        return CityViewHolder(LayoutInflater.from(context).inflate(R.layout.weather_city_manager_list_item, parent, false))
    }

    override fun onBindViewHolder(holder: CityViewHolder, position: Int) {
        holder.cityName.text = data[position].name
        holder.cityDelete.setOnClickListener{
            AlertDialog.Builder(context)
                .setTitle("删除提示")
                .setMessage("是否删除城市：" + data[position].name)
                .setPositiveButton("确定") { _, _ ->
                    run {
                        val cityList = viewMode.getCurrentWeatherCityInfoList()
                        var removeCityIndex = -1

                        cityList.forEachIndexed { index, weatherCityInfo ->
                            if (weatherCityInfo.cityId == data[position].cityId){
                                removeCityIndex = index
                                return@forEachIndexed
                            }
                        }

                        if(removeCityIndex != -1){
                            if (cityList.size == 1){
                                Toast(context).also {
                                    it.setText("再删除就没有了哦")
                                    it.duration = Toast.LENGTH_SHORT
                                    it.show()
                                }
                            }else {
                                cityList.removeAt(removeCityIndex)
                                viewMode.updateCurrentWeatherCityInfoList(cityList)

                                updateData(cityList)
                                this.notifyDataSetChanged()
                            }
                        }
                    }
                }
                .setNegativeButton("取消") { _, _ ->
                    run {

                    }
                }.show()
        }
    }

    override fun getItemCount(): Int {
        Log.i(TAG, "getItemCount: " + data.size)
        return data.size
    }

    fun updateData(updateData: List<WeatherCityInfo>){
        data = updateData
    }

}